package com.vst.test.generate.structure.springmvc.plugin.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

public class Util {
	public static String getStringFromInputStream(InputStream is) {
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		String line;
		try {
			br = new BufferedReader(new InputStreamReader(is));
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

	public static File getFileProject(IProject project, String ruta) {
		return new File(project.getFile(ruta).getLocationURI().getPath());
	}

	public static Object getObjectXMLInProject(Class<?> objclass, File file) throws JAXBException {
		JAXBContext jaxbContext = JAXBContext.newInstance(objclass);
		Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
		return jaxbUnmarshaller.unmarshal(file);
	}

	public static void generateObjecttoXMLInProject(EstructureObject customer, Class<EstructureObject> objclass, File file) throws JAXBException {
		JAXBContext jaxbContext = JAXBContext.newInstance(objclass);
		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
		jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		jaxbMarshaller.marshal(customer, file);
		jaxbMarshaller.marshal(customer, System.out);
	}

	public static String getNameFile(String elementName) {
		return elementName.substring(0, elementName.indexOf("."));
	}

	public static String getCamporDesdeMetodoGET(String elementName) {
		elementName = elementName.substring(3, elementName.length());
		elementName = Character.toLowerCase(elementName.charAt(0)) + (elementName.length() > 1 ? elementName.substring(1) : "");
		return elementName;
	}

	public static String capitalizarPrimerCaracter(String elementName) {
		elementName = Character.toUpperCase(elementName.charAt(0)) + (elementName.length() > 1 ? elementName.substring(1) : "");
		return elementName;
	}

	public static void formatUnitSourceCode(ICompilationUnit cu, IProgressMonitor monitor) throws JavaModelException {
		// CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
		// ISourceRange range = cu.getSourceRange();
		// TextEdit formatEdit =
		// formatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(),
		// range.getOffset(), range.getLength(), 0, null);
		// if (formatEdit != null && formatEdit.hasChildren()) {
		// unit.applyTextEdit(formatEdit, monitor);
		// } else {
		// monitor.done();
		// }

		CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);

		TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, cu.getSource(), 0, cu.getSource().length(), 0, null);
		IDocument doc = new Document(cu.getSource());
		try {
			textEdit.apply(doc);

			cu.applyTextEdit(textEdit, monitor);

			System.out.println(doc.get());
		} catch (MalformedTreeException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	public static void commitCompilationUnit(ICompilationUnit src) throws Exception {
		final ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		parser.setSource(src);
		parser.setResolveBindings(true); // we need bindings later on
		CompilationUnit node = (CompilationUnit) parser.createAST(null);

		Document document = new Document(src.getBuffer().getContents());
		TextEdit edits = node.rewrite(document, src.getJavaProject().getOptions(true));
		edits.apply(document);
		src.getBuffer().setContents(document.get());
		if (src.isWorkingCopy()) {
			src.commitWorkingCopy(false, null);
		}
		src.save(null, false);
	}

//	public static void main(String[] args) {
//		String code = "public class TestFormatter{public static void main(String[] args){System.out.println(\"Hello World\");}}";
//		CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
//
//		TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0, code.length(), 0, null);
//		IDocument doc = new Document(code);
//		try {
//			textEdit.apply(doc);
//			System.out.println(doc.get());
//		} catch (MalformedTreeException e) {
//			e.printStackTrace();
//		} catch (BadLocationException e) {
//			e.printStackTrace();
//		}
//	}

	public static String getTipoparamtroInstanceOf(String typeSignature) {
		if (typeSignature.indexOf("Boolean") > 0) {
			return "Boolean";
		}
		if (typeSignature.indexOf("String") > 0) {
			return "String";
		}
		if (typeSignature.indexOf("Date") > 0) {
			return "Date";
		}
		if (typeSignature.indexOf("int") > 0) {
			return "int";
		}
		if (typeSignature.indexOf("boolean") > 0) {
			return "boolean";
		}
		if (typeSignature.indexOf("char") > 0) {
			return "char";
		}
		if (typeSignature.indexOf("Character") > 0) {
			return "Character";
		}
		if (typeSignature.indexOf("Long") > 0) {
			return "Long";
		}
		if (typeSignature.indexOf("long") > 0) {
			return "long";
		}
		return "Object";
	}

}
